package com.isoft.student.controller;

import com.isoft.student.eo.TeacherEOPage;
import com.isoft.student.model.Teacher;
import com.isoft.student.service.TeacherService;
import com.isoft.student.tool.ResponseMessage;
import com.isoft.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据接口
 */
@Api(tags = "teacher")
@RestController
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "教师信息添加方法")
    @RequestMapping(value = "/addTeacher", method = RequestMethod.GET)
    public int AddTeacher(Teacher teacher) {
        int IsAdd = 0;
        //传入数据的判断
        //1.实体对象不能为空
        if (teacher != null) {
            //2.教师名称不能为空
            if (teacher.getTnumber() != null) {
                //添加教师信息
                IsAdd = teacherService.AddTeacher(teacher);
            }
        }
        return IsAdd;
    }

    @ApiOperation(value = "教师信息修改方法")
    @RequestMapping(value = "/updateTeacher", method = RequestMethod.GET)
    public ResponseMessage updateTeacher(Teacher teacher){
        //传入的参数不能为空
        if (teacher!=null){
            //参数中教师号不能为空
            if (teacher.getTid()!=0){
               if (teacherService.UpdateTeacher(teacher)==1){
                   if (teacher.getTnumber()!=null){
                       if (teacher.getTeaching()!=null){
                       }
                   }
                   return Result.success("更新成功");
               }else{
                   return Result.success("更新失败");
               }
            }
        }
        return Result.success("传入参数不正确");
    }

    @ApiOperation(value = "教师信息删除方法")
    @RequestMapping(value = "/delTeacher", method = RequestMethod.GET)
    public ResponseMessage delTeacher(int perid){
        if (perid!=0){
            int delInt=0;
            delInt=teacherService.DeleteTeacher(perid);
            if (delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为0，请重新输入");
        }
    }

    @ApiOperation(value = "根据条件获取教师信息")
    @RequestMapping(value = "/getTeacherWhereAll",produces = "application/json;charset=utf-8",method=RequestMethod.PUT)
    private ResponseMessage getAll(TeacherEOPage teacherEOPage){
        Map<String, List> map=new HashMap<>();
        List<TeacherEOPage> listTeacher=teacherService.getTeacherWhereAll(teacherEOPage);
        map.put("list",listTeacher);
        return Result.success("200",map);
    }
}